#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/6/7 9:02
@File: otherUtils.py
"""
import random
from jsonpath import jsonpath as jp

def random_number(length):
    '''
    生成指定长度的随机数字符串
    '''
    rtn=''
    _=['0','1','2','3','4','5','6','7','8','9']
    for i in range(length):
        rtn+=random.choice(_)
    return rtn

def random_str(length,mode='0|a|A'):
    '''
    length:长度
    mode:0-数字,a-小写英文,A-大写英文,|分隔
    '''
    num='0123456789'
    low='abcdefghijklmnopqrstuvwxyz'
    up='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    source=''
    rtn=''
    for i in mode.split('|'):
        if i=='0':
            source+=num
        elif i=='a':
            source+=low
        elif i=='A':
            source+=up
    for i in range(length):
        rtn+=random.choice(source)
    return rtn


def merge(templatedata,**kwargs):
    '''
    根据jsonpath替换
        kwargs:
            replaceAll:默认为False根据jsonpath替换第一个，true替换所有
    '''
    replaceAll=kwargs.get('replaceAll',False)
    for k,v in kwargs.items():
        path=jp(templatedata,f"$..['{k}']",result_type='PATH')
        if path:
            if not replaceAll:
                path=[path[0]]
            for i in path:
                if isinstance(v,str):
                    if "'" in v:
                        exec(f'templatedata{i[1:]}="""{v}"""')
                    else:
                        exec(f"templatedata{i[1:]}='''{v}'''")
                else:
                    exec(f"templatedata{i[1:]}={v}")
    return templatedata


if __name__ == '__main__':
    # print(random_number(5))
    # _={
    #     '账户组名称': 'UI测试',
    #     '账户组维度': '客户号',
    #     '账户组类型': '炒新账户组',
    #     '是否公共': '否',
    #     '是否启用': '是',
    #     '备注': '123'
    # }
    # __={
    #     '账户组名称': '__del',
    #     '账户组维度': '2',
    #     '是否公共': 4,
    #     '是否启用': '5',
    #     '备注': '6'
    # }
    # print(merge(_,**__))
    print(random_str(20,seed='1'))
